﻿using UnityEngine;

namespace Share
{
    /// <summary>
    /// 管理游戏中的各种根节点
    /// </summary>
    public class GameRootManager : SingletonObject<GameRootManager>
    {
        private GameObject m_ObjectRoot;
        private GameObject m_CityObjectRoot;
        private GameObject m_SkillRoot;
        private GameObject m_BulletRoot;
        private GameObject m_StateRoot;
        private GameObject m_EffectRoot;
        private GameObject m_ColliderRoot;
        private GameObject m_LineRoot;
        private GameObject m_DamageRoot;
        private GameObject m_PoolRoot;

        /// <summary>
        /// 获取技能根节点
        /// </summary>
        /// <returns>技能根节点Transform</returns>
        public Transform GetObjectRoot() => GetOrCreateRoot(ref m_ObjectRoot, "ObjectRoot");
        public Transform GetCityObjectRoot() => GetOrCreateRoot(ref m_CityObjectRoot, "CityObjectRoot");

        public Transform GetBulletRoot() => GetOrCreateRoot(ref m_BulletRoot, "BulletRoot");

        /// <summary>
        /// 获取技能根节点
        /// </summary>
        /// <returns>技能根节点Transform</returns>
        public Transform GetSkillRoot() => GetOrCreateRoot(ref m_SkillRoot, "SkillRoot");

        /// <summary>
        /// 获取状态根节点
        /// </summary>
        /// <returns>状态根节点Transform</returns>
        public Transform GetStateRoot() => GetOrCreateRoot(ref m_StateRoot, "StateRoot");

        /// <summary>
        /// 获取特效根节点
        /// </summary>
        /// <returns>特效根节点Transform</returns>
        public Transform GetEffectRoot() => GetOrCreateRoot(ref m_EffectRoot, "EffectRoot");
        /// <summary>
        /// 获取画线 根节点
        /// </summary>
        /// <returns>特效根节点Transform</returns>
        public Transform GetLineRoot() => GetOrCreateRoot(ref m_LineRoot, "LineRoot");

        public Transform GetDamageRoot() => GetOrCreateRoot(ref m_DamageRoot, "DamageRoot");

        /// <summary>
        /// 获取碰撞体根节点
        /// </summary>
        /// <returns>碰撞体根节点Transform</returns>
        public Transform GetColliderRoot() => GetOrCreateRoot(ref m_ColliderRoot, "ColliderRoot");

        /// <summary>
        /// 清理特效根节点
        /// </summary>
        public void ClearEffectRoot() => ClearRoot(ref m_EffectRoot);

        public Transform GetPoolRoot() => GetOrCreateRoot(ref m_PoolRoot, "PoolRoot");
        public void ClearPoolRoot() => ClearRoot(ref m_PoolRoot);

        /// <summary>
        /// 清理碰撞体根节点
        /// </summary>
        public void ClearColliderRoot() => ClearRoot(ref m_ColliderRoot);

        private Transform GetOrCreateRoot(ref GameObject root, string name)
        {
            if (root == null)
            {
                root = new GameObject(name);
                //Object.DontDestroyOnLoad(root);
            }
            return root.transform;
        }

        private void ClearRoot(ref GameObject root)
        {
            if (root != null)
            {
                GameObject.Destroy(root);
                root = null;
            }
        }
    }
}
